package org.example.config;

import lombok.Data;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author 李荣
 * @Date 2025/1/3
 * @Description
 */
@Configuration
@ConfigurationProperties(prefix = "mqtt")
@Data
public class MqttConfig {

    private String brokerUrl;
    private String clientId;
    private String username;
    private String password;
    private Integer keepAlive;
    private Boolean cleanSession;

    @Bean
    public MqttClient mqttClient() throws MqttException {
        MqttConnectOptions connectOptions = new MqttConnectOptions();
//        connectOptions.setUserName(username);
//        connectOptions.setPassword(password.toCharArray());
        connectOptions.setKeepAliveInterval(keepAlive);
        connectOptions.setCleanSession(cleanSession);
        connectOptions.setAutomaticReconnect(true);


        MqttClient client = new MqttClient(brokerUrl,clientId,new MemoryPersistence());
        //client.subscribe("cc");
        client.connect(connectOptions);

        return client;
    }
}
